//
//  WXPMineVC.m
//  MexicoLoan
//
//  Created by wxp on 2025/2/25.
//

#import "WXPMineVC.h"
#import "WXPMineHeaderView.h"
#import "WXPCreditReportDetailVC.h"

@interface WXPMineVC ()


@property (nonatomic, strong) UIImageView * viewTopBg;
@property (nonatomic, strong) WXPMineHeaderView * viewHeader;
@property (nonatomic, strong) ModelGetCRSource * getCRSourceModel;
@property (nonatomic, strong) WXPTwoBtnPopUpWindowView * ficoInfoErrorView;

@end

@implementation WXPMineVC

#pragma mark - lazy
- (UIImageView *)viewTopBg{
    if (!_viewTopBg) {
        _viewTopBg = [UIImageView new];
        _viewTopBg.widthHeight = XY(SCREEN_WIDTH, SCREEN_WIDTH/375*252);
        _viewTopBg.leftTop = XY(0, 0);
        _viewTopBg.image = [UIImage imageNamed:@"icon-mine-top-bg"];
    }
    return _viewTopBg;
}
- (WXPMineHeaderView *)viewHeader
{
    if (_viewHeader == nil) {
        _viewHeader = [WXPMineHeaderView new];
        WEAKSELF
        _viewHeader.blockResetHeaderView = ^{
            weakSelf.tableView.tableHeaderView = weakSelf.viewHeader.viewContent;
        };
    }
    return  _viewHeader;
}
- (WXPTwoBtnPopUpWindowView *)ficoInfoErrorView{
    if (!_ficoInfoErrorView) {
        _ficoInfoErrorView = [WXPTwoBtnPopUpWindowView new];
        ModelBtn * model = [ModelBtn new];
        model.imageName = @"icon-KTP-fail";
        model.content = @"El puntaje FICO ha sido pagado, pero no se ha generado ningún informe. Por favor, actualice su información personal para aprobarlo de nuevo.";
        model.rightName = @"Ir a modificar";
        [_ficoInfoErrorView resetWithModel:model];
        [_ficoInfoErrorView resetOnlyShowRightBtn];
        WEAKSELF
        _ficoInfoErrorView.blockRightClick = ^{
            [weakSelf.ficoInfoErrorView removeFromSuperview];
            WXPCreditReportDetailVC * vc = [WXPCreditReportDetailVC new];
            vc.type = ENUM_REPORT_FICO;
            [Global_Nav pushViewController:vc animated:true];
        };
    }
    return _ficoInfoErrorView;
}


#pragma mark - viewDidLoad
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    // request
    [GlobalMethod hitPointWithPageName:KEY_Mine action:KEY_Enter extInfo:nil];
    // 请求用户信息
    [self requestUserInfo];
    [self requestIsShowFAQEntrance];
    [self requestIsShowCREntrance];
    [self requestBannerUrl];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // 注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jumpToHomePageVC) name:JUMP_TO_HOMEPAGE object:nil];
    
    [self configUI];
}

- (void)configUI{
    CGFloat bottom = 50+([GlobalMethod getBottomSafeAreaHeight] == 0 ? LEFT_MARDIN : [GlobalMethod getBottomSafeAreaHeight]);
    self.tableView.height = SCREEN_HEIGHT-bottom-12-[GlobalMethod getStatusBarHeight];
    self.tableView.top = [GlobalMethod getStatusBarHeight];
    self.viewBG.backgroundColor = COLOR_BACKGROUND;
    self.tableHeaderTopBg.backgroundColor = [UIColor clearColor];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.viewBG addSubview:self.viewTopBg];
    self.tableView.tableHeaderView = self.viewHeader.viewContent;
}

- (void)jumpToHomePageVC{
    [self.tabBarController setSelectedIndex:0];
}

#pragma mark - request
- (void)requestUserInfo{
    [RequestApi requestUserInformationWithDelegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        [self.viewHeader.viewUserInfo resetView];
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}
// banner
- (void)requestBannerUrl {
    [RequestApi requestAppBannerUrlWithPage:2 delegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        NSMutableArray * bannerAry = [NSMutableArray new];
        [bannerAry removeAllObjects];
        NSArray * aryResponse = [GlobalMethod exchangeDic:response toAryWithModelName:@"ModelBanner"];
        if (isAry(aryResponse)) {
            [bannerAry addObjectsFromArray: aryResponse];
            [self.viewHeader.bannerView resetViewWithAry:bannerAry];
            [self.viewHeader resetHeaderView];
        }
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}
// 是否展示信用报告
- (void)requestIsShowCREntrance{
    [RequestApi requestIsShowCREntranceWithPartnerId:@"2fec752cc5d9a2774911936cfb21dd39" pageType:[NSNumber numberWithInteger:2] delegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {

        NSString * numStr = [NSString stringWithFormat:@"%@",response];
        if([numStr boolValue]){
            // 显示 信用报告
            [self requestCRSource];
        }else{
            // 隐藏 信用报告（FICO和Cloudun都不显示了）
            self.viewHeader.viewMiCredito.height = 0;
            [self.viewHeader resetHeaderView];
        }

    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {

    }];
}
// 信用报告分数
- (void)requestCRSource{
    [RequestApi requestCRSourceWithPartnerId:@"2fec752cc5d9a2774911936cfb21dd39" delegate:nil success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {

        self.getCRSourceModel = [GlobalMethod exchangeDicToModel:response modelName:@"ModelGetCRSource"];
        if(self.getCRSourceModel.ficoInfoError){
            WEAKSELF
            [GlobalMethod executeOncePerDayWithKey:LOCAL_FICOINFOERROR_SHOWTIME block:^{
                UIWindow * window = [UIApplication sharedApplication].windows.firstObject;
                [window addSubview:weakSelf.ficoInfoErrorView];
            }];
        }
        [self.viewHeader.viewMiCredito resetViewWithModel: self.getCRSourceModel];
        [self.viewHeader resetHeaderView];

    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {

    }];
}

// 是否展示FAQ
- (void)requestIsShowFAQEntrance{
    [RequestApi requestIsShowFAQEntranceWithDelegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        NSString * numStr = [NSString stringWithFormat:@"%@",response];
        if([numStr boolValue]){
            // 显示FAQ
            [self.viewHeader.viewFAQ resetViewWithShowFAQ:true];
        }else{
            // 隐藏FAQ
            [self.viewHeader.viewFAQ resetViewWithShowFAQ:false];
        }
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}

@end
